#!/bin/sh

COMM_DIST=../dist
MEMO_FILE=${COMM_DIST}/bin/memo.txt
VER_FILE=${COMM_DIST}/bin/version.txt
FLAG_DEBUG=R
DIST_TAR_DIR=""
APM_VER=V2.0.R5.1
. ./cfg_modules

#清除老文件
dist_clear()
{
	rm ${COMM_DIST}/conf -rf
	rm ${COMM_DIST}/dev -rf
	rm ${COMM_DIST}/lib -rf
	rm ${COMM_DIST}/logs -rf
	rm ${COMM_DIST}/temp -rf
	rm ${COMM_DIST}/modules -rf
	rm ${COMM_DIST}/script -rf
	find ${COMM_DIST} -name ".svn" | xargs rm -rf
	
	mkdir -p ${COMM_DIST}/bin
	return 1;
}

#程序安装
dist_install()
{
	# lualib部分发布
	cp ../lualib ${COMM_DIST} -rp
	cp ../lua ${COMM_DIST} -rp

	# 应用程序和协议解析库发布
	cd lib
	cd apmcomm/;make install;make DEFINE_C=true install
	cd ../apmmodule/;make install
	cd ../apmshm/;make install
	cd ../report/; make DEFINE_NO_SEND=true install; make install
	cd ../apm4lua/;make install
	cd ../..
	
	cd exec
	cd framework;make install
	cd ../client;make install
	cd ../../

	cd module
	for module_name in ${modules_list[@]}  
	do  
		cd ${module_name}
		make install
		cd ..
	done
	cd ../
	
	return 1;
}

#生成压缩文件包
dist_tar()
{
	# 运行程序打包
	DIST_TAR_DIR=${COMM_DIST}"/../Broadview_APM/APM_"${APM_VER}
	local var_dist_tar_file="uyun-apm-broker-"${APM_VER}".tar.gz"
	
	find ${COMM_DIST} -name ".svn" | xargs rm -rf
	
	# 生成运行框架
	rm -rf ${DIST_TAR_DIR}; mkdir -p ${DIST_TAR_DIR}
	
	# 拷贝协议安装包
	find ${COMM_DIST} -name "*.mod" | xargs -I {} mv {} ${DIST_TAR_DIR}/.
	# rename .zip  .mod  ${DIST_TAR_DIR}/*.zip

	echo "tar -czvf ${DIST_TAR_DIR}/$var_dist_tar_file ${COMM_DIST}"
	tar -czvf ${DIST_TAR_DIR}/$var_dist_tar_file ${COMM_DIST} > /dev/null 2>&1
	
	date > ${DIST_TAR_DIR}/memo.txt
	cd -
	
	cd ../
	local devel_tar_file="uyun-apm-broker-devel-"${APM_VER}".tar.gz"
	tar -czvf ${DIST_TAR_DIR}/$devel_tar_file ../src/module/sample ../conf/modules/14
	
	return 1;
}

#程序开始运行

# 获取参数 $1:D表示debug版本
if [ $# -ge 1 ]; then
    if [ "$1" == "D" -o "$1" == "-D" -o "$1" == "d" -o "$1" == "-d" ]; then
        FLAG_DEBUG=D
        echo "注意:内部测试版本"
    fi
fi

#环境清理
dist_clear;

#复制运行脚本
rm ${COMM_DIST}/script/* -rf; mkdir -p ${COMM_DIST}/script; cp ../script/* ${COMM_DIST}/script -rp; chmod +x ${COMM_DIST}/script/*

#生成配置文件
rm ${COMM_DIST}/conf/* -rf; mkdir -p ${COMM_DIST}/conf; cp ../conf/* ${COMM_DIST}/conf -rp
# 清除modules
rm -rf ${COMM_DIST}/conf/modules

rm -f ${COMM_DIST}/conf/analyzer_*.xml
rm -f ${COMM_DIST}/conf/start_module_*.xml
if [ "$FLAG_DEBUG" == "D"  ]; then
	rm -f ${COMM_DIST}/conf/analyzer.xml
	rm -f ${COMM_DIST}/conf/start_module.xml
	cp ../conf/analyzer_debug.xml ${COMM_DIST}/conf/analyzer.xml
	cp ../conf/start_module_debug.xml ${COMM_DIST}/conf/start_module.xml
fi
mkdir -p ${COMM_DIST}/logs

date > $MEMO_FILE
echo >> $MEMO_FILE
svn info . | tail -n 3 >> $MEMO_FILE

svn info . | tail -n 3| head -n 1 | awk -F" " '{print "svn:" $2}' > $VER_FILE

#文件保存到指定目录
dist_install;

# 文件压缩
dist_tar;


echo ""
echo "========================================================="
echo "打包文件位置"
echo "   ${DIST_TAR_DIR}"
echo ""
echo "运行方法：进入下面目录"
echo "cd ${COMM_DIST}/bin"
echo ""
echo "eg."
echo "   #配置lua运行环境变量"
echo "      export LUA_PATH=\"../lib/?.lua;../lualib/framework/?.lua;../lualib/lib/?.lua;/usr/share/lua/5.1/?.lua;${LUA_PATH}\""
echo "      export LUA_CPATH=\"../lib/?.so;../lualib/lib/?.so;/usr/lib/lua/5.1/?.so;${LUA_CPATH}\""
echo ""
echo "   rm ../logs/* -f; ./apm  -n -i eth (网卡)"
echo "   rm ../logs/* -f; ./apm  -n -d ../../src/test_data/mssql/nolongrpc.pcap"
echo ""

#rm ${COMM_DIST}/* -rf

